package chapt04.solar;
// Solar.java
// OpenGL SuperBible
// Demonstrates OpenGL nested coordinate transformations and perspective
// Program by Richard S. Wright Jr.

import javax.media.opengl.GL2;
import javax.media.opengl.glu.GLU;

import com.jogamp.opengl.util.gl2.GLUT;

public class Solar
{
	static GLU glu = new GLU();
	static GLUT glut = new GLUT();
	
	// Lighting values
	static float whiteLight[] = {0.2f, 0.2f, 0.2f, 1.0f};
	static float sourceLight[] = {0.8f, 0.8f, 0.8f, 1.0f};
	static float lightPos[] = {0.0f, 0.0f, 0.0f, 1.0f};
	
	// Earth and Moon angle of revolution
	static float fMoonRot = 0.0f;
	static float fEarthRot = 0.0f;
	
	// This function does any needed initialization on the rendering context.
	protected static void setup(GL2 gl2, int w, int h)
	{	
		// Light values and coordinates
		gl2.glEnable(GL2.GL_DEPTH_TEST); // Hidden surface removal
		gl2.glFrontFace(GL2.GL_CCW);     // Counter clock-wise polygons face out
		gl2.glEnable(GL2.GL_CULL_FACE);  // Do not calculate inside of jet
		
		// Enable lighting
		gl2.glEnable(GL2.GL_LIGHTING);
		
		// Setup and enable light 0
		gl2.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, whiteLight, 0);
		gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, sourceLight, 0);
		gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, lightPos, 0);
		gl2.glEnable(GL2.GL_LIGHT0);
		
		// Enable color tracking
		gl2.glEnable(GL2.GL_COLOR_MATERIAL);
		
		// Set Material properties to follow glColor values
		gl2.glColorMaterial(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE);
		
		// Black blue background
		gl2.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	}
	
	// Called to draw scene
	protected static void render(GL2 gl2, int w, int h)
	{
		float fAspect;
		
		// Prevent a divide by zero
		if(h == 0)
			h = 1;
		
		// Set Viewport to window dimensions
		gl2.glViewport(0, 0, w, h);
		
		// Calculate aspect ratio of the window
		fAspect = (float)w / (float)h;
		
		// Set the perspective coordinate system
		gl2.glMatrixMode(GL2.GL_PROJECTION);
		gl2.glLoadIdentity();
		
		// field of view of 45 degrees, near and far planes 1.0 and 425
		glu.gluPerspective(45.0f, fAspect, 1.0, 425.0);
		
		// Modelview matrix reset
		gl2.glMatrixMode(GL2.GL_MODELVIEW);
		gl2.glLoadIdentity();
		
		// Clear the window with current clearing color
		gl2.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
		
		// Save the matrix state and do the rotations
		gl2.glMatrixMode(GL2.GL_MODELVIEW);
		gl2.glPushMatrix();
		
		// Translate the whole scene out and into view
		gl2.glTranslatef(0.0f, 0.0f, -300.0f);
		
		// Set material color, Red
		// Sun
			gl2.glDisable(GL2.GL_LIGHTING);
		gl2.glColor3ub((byte)255, (byte)255, (byte)0);
		glut.glutSolidSphere(15.0f, 30, 17);
			gl2.glEnable(GL2.GL_LIGHTING);
			
		// Move the light after we draw the sun!
		gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, lightPos, 0);
		
		// Rotate the coordinate system
		gl2.glRotatef(fEarthRot, 0.0f, 1.0f, 0.0f);
		
		// Draw the earth
		gl2.glColor3ub((byte)0, (byte)0, (byte)255);
		gl2.glTranslatef(105.0f, 0.0f, 0.0f);
		glut.glutSolidSphere(15.0f, 30, 17);
		
		// Rotate from Earth based coordinates and draw Moon
		gl2.glColor3ub((byte)200, (byte)200, (byte)200);
		gl2.glRotatef(fMoonRot, 0.0f, 1.0f, 0.0f);
		gl2.glTranslatef(30.0f, 0.0f, 0.0f);
		fMoonRot += 3.0f;
		if(fMoonRot > 360.0f)
			fMoonRot -= 360.0f;
		
		glut.glutSolidSphere(6.0f, 30, 17);
		
		// Restore the matrix state
		gl2.glPopMatrix(); // Modelview matrix
		
		// Step earth orbit 1 degrees
		fEarthRot += 1.0f;
		if(fEarthRot > 360.0f)
			fEarthRot -= 360.0f;
		
		// Show the image
		gl2.glFlush();
	}
}